/* eslint no-unused-vars:0 */
import React from 'react';
import PubSub from 'pubsub-js';
import 'antd/dist/antd.css';
import './index.less';
import { Table, Menu } from 'antd';

import { Link } from 'react-router-dom';
import PDFViewer from './../Backmanage/PublicOpinionReportManage/Report/PDFViewer';
import SituationTable from './SituationTable';
import WorkBrieging from './WorkBriefing/WorkBriefing';
import ExpertManuscripts from './ExpertManuscripts/ExpertManuscripts';

export default class RiskAssessment extends React.Component {
    state = {
        module: [],
        data: [],
        columns: [
            {
                title: '舆情报告集',
                key: 'originalName',
                inputType: 'input',
                dataIndex: 'originalName',
                editable: true
            },
            {
                title: '作者',
                key: 'authorName',
                inputType: 'input',
                dataIndex: 'authorName',
                editable: true
            },
            {
                title: '预览',
                key: 'reportFile',
                render: (text, record) => {
                    return (
                        <div>
                            <a onClick={this.readPdf.bind(text, record)}>
                                查看文档内容
                            </a>
                        </div>
                    );
                }
            }
        ],
        page: 1,
        size: 5,
        total: 0,
        selectedKeys: ['0'],
        publicSentiment: {
            id: ''
        }
    };

    constructor(props) {
        super(props);
    }

    componentDidMount() {
        this.getData();
    }

    getData = () => {
        this.$post('userAndSentiment/getSentimentByUser')
            .then((res) => {
                if (res) {
                    this.getReport(res.result[0]);
                    this.setState({
                        module: res.result
                    });
                } else this.$showNotification('error', '获取数据失败');
            })
            .catch((e) => {
                console.log(e);
            });
    };

    getReport = (data, record) => {
        this.setState({
            publicSentiment: data
        });
        if (record != undefined) {
            var keys = [`${record}`];
            this.setState({
                selectedKeys: keys
            });
        }
        this.$post('report/findByPublic', {
            publicSentimentId: data.id,
            page: this.state.page,
            size: this.state.size
        })
            .then((res) => {
                if (res) {
                    var data = res.obj;
                    for (var i = 0; i < data.length; i++) {
                        data[i]['key'] = data[i].id;
                    }
                    this.setState({
                        data: data,
                        total: res.maxCount
                    });
                } else this.$showNotification('error', '获取数据失败');
            })
            .catch((e) => {
                console.log(e);
            });
    };

    getPageReport = (page) => {
        this.$post('report/findByPublic', {
            publicSentimentId: this.state.publicSentiment.id,
            page: page,
            size: this.state.size
        })
            .then((res) => {
                if (res) {
                    var data = res.obj;
                    for (var i = 0; i < data.length; i++) {
                        data[i]['key'] = data[i].id;
                    }
                    this.setState({
                        data: data,
                        total: res.maxCount
                    });
                } else this.$showNotification('error', '获取数据失败');
            })
            .catch((e) => {
                console.log(e);
            });
    };

    readPdf = (text, record) => {
        PubSub.publish('readPdf', text.saveName);
        sessionStorage.setItem('readPdf', text.saveName);
    };

    pageChange = (page, size) => {
        this.setState({
            page: page
        });
        this.getPageReport(page);
    };

    openTable = () => {
        this.setState({
            selectedKeys: ['0']
        });
    };

    openEM = () => {
        this.setState({
            selectedKeys: ['1']
        });
    };

    render() {
        const MenuItem = this.state.module.map((d, index) =>
            d.name == '工作简报' || d.name == '专家约稿' ? (
                ''
            ) : (
                <Menu.Item key={index + 3}>
                    <a onClick={this.getReport.bind(this, d, index + 3)}>
                        {d.name}
                    </a>
                </Menu.Item>
            )
        );

        const content =
            this.state.selectedKeys[0] === '0' ? (
                <WorkBrieging />
            ) : this.state.selectedKeys[0] === '1' ? (
                <ExpertManuscripts />
            ) : (
                <Table
                    columns={this.state.columns}
                    dataSource={this.state.data}
                    pagination={{
                        current: this.state.page,
                        pageSize: this.state.size,
                        total: this.state.total,
                        onChange: this.pageChange
                    }}
                />
            );

        return (
            <div className="body">
                <div style={{ marginTop: '20px' }}>
                    <span className="opinion-body">舆情报告</span>
                </div>
                <Menu
                    mode="horizontal"
                    defaultSelectedKeys={this.state.selectedKeys}>
                    <Menu.Item key={0}>
                        <a onClick={this.openTable}>工作简报</a>
                    </Menu.Item>
                    <Menu.Item key={1}>
                        <a onClick={this.openEM}>专家约稿</a>
                    </Menu.Item>
                    {MenuItem}
                </Menu>

                {content}

                <PDFViewer />
            </div>
        );
    }
}
